package IMIC.NGUYENVANDOAN.SERIZABLEFILES;

import java.util.ArrayList;
import java.util.List;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;

public class VIEW_READFILES {
    public static void main(String[] args) {
        JFileChooser objFile = new JFileChooser();
        objFile.setCurrentDirectory(new java.io.File("."));
        objFile.setDialogTitle("Chọn file: ");
        objFile.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        String[] EXTENSION = new String[] { "dat" };
        FileNameExtensionFilter filter = new FileNameExtensionFilter("DAT files", EXTENSION);
        objFile.setFileFilter(filter);
        objFile.setMultiSelectionEnabled(false);

        int returnVal = objFile.showOpenDialog(null);
        String ParentPath = "";
        String pathFile = "";
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            ParentPath = objFile.getSelectedFile().getParent();
            pathFile = objFile.getSelectedFile().getPath();

            // 1. Khai báo & khởi tạo controllers
            CONTROLLERS objController = new CONTROLLERS();

            // 2. Đọc dữ liệu trong files *.dat --> bindding vào List<Student>
            List<Student> lisOut = new ArrayList<Student>();
            try {
                lisOut = objController.ReadObjects(pathFile);

                // 3. Duyệt & hiển thị dữ liệu từ list<Student> ra bên ngoài
                for (int i = 0; i < lisOut.size(); i++) {
                    System.out.println("----------Student: " + lisOut.get(i).getFullName() + " ------------");
                    System.out.println("+ RollNo: " + lisOut.get(i).getRollNo());
                    System.out.println("+ FullName: " + lisOut.get(i).getFullName());
                    System.out.println("+ Age: " + lisOut.get(i).getAge());
                    System.out.println("+ Phone: " + lisOut.get(i).getPhone());
                }
            } catch (Exception e) {
                System.out.println("Err" + e.getMessage());
            }
        }

    }
}
